package com.practice.Socket;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class ServerProcessor {
    private Map<String, PrintWriter> printWriterHashMap = new HashMap<String, PrintWriter>();

    class SocketReceiver implements Runnable {
        InputStream inputStream;
        Integer threadNo;
        Integer closeInfo;
        String cs;

        public SocketReceiver(InputStream reader, Integer threadNo, Integer closeInfo, String cs) {
            this.inputStream = reader;
            this.threadNo = threadNo;
            this.closeInfo = closeInfo;
            this.cs = cs;
        }

        @Override
        public void run() {
            try {
                while (true) {
                    Integer count = inputStream.available();
                    if (count == 0) {
                        Thread.sleep(500);
                        continue;
                    }
                    byte[] b = new byte[count];
                    int readCount = 0; // 已经成功读取的字节的个数
                    String content = "";
                    while (readCount < count) {
                        readCount += inputStream.read(b, readCount, count - readCount);
                        content += new String(b, "UTF-8");
                    }
                    System.out.println(cs + " NO." + threadNo + "接收 内容: " + content);
                    if (content.equalsIgnoreCase("end")) {
                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                closeInfo += 1;
            }
        }
    }

    class SocketSender implements Runnable {

        public SocketSender() {
        }


        @Override
        public void run() {
            BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
            String content;
            try {
                content = sysin.readLine();
                while (true) {
                    if (content.length() > 0) {
                        if (content.equalsIgnoreCase("end")) {
                            break;
                        }
                        for (PrintWriter writer : printWriterHashMap.values()) {
                            writer.print(content);
                            writer.flush();
                        }
                    } else {
                        Thread.sleep(500);
                    }
                    content = sysin.readLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                try {
                    sysin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public Map<String, PrintWriter> getPrintWriterHashMap() {
        return printWriterHashMap;
    }
}
